package com.yn.xgame.logic;

import com.yn.xgame.CommonMqMessageHandler;
import com.yn.xgame.MQClient;
import com.yn.xgame.ZkClient;
import com.yn.xgame.game.GameCenterWatcher;
import com.yn.xgame.logic.handler.ShuiHuHandler;
import com.yn.xgame.logic.util.JedisUtils;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * LogicApp
 * Created by wangboo on 17/7/13.
 */
public class LogicApp implements Watcher {

    private static final Logger logger = LoggerFactory.getLogger(LogicApp.class);
    static LogicServer logicServer;
    static DbServer dbServer;
    static AssistServer assistServer;
    static LogicConfig config;
    static MQClient mqClient;
    static ZkClient zkClient;
    private static GameCenterWatcher gameCenterWatcher;

    public static void main(String[] args) throws Exception {
        String file = "logic1.cfg";
        if (args.length > 0) file = args[0];
        config = new LogicConfig("logic", file);
        logicServer = new LogicServer();
        dbServer = new DbServer();
        assistServer = new AssistServer();
        zkClient = new ZkClient();
        JedisUtils.init(config.getRedisIp(), config.getRedisPort());
        gameCenterWatcher = new GameCenterWatcher(zkClient);
        zkClient.connect(config.getZkAddress(), gameCenterWatcher);
        mqClient = new MQClient(config.getKafkaProp(), new CommonMqMessageHandler(logicServer), config.getMqGroup(),
                config.getTopic());
        gameCenterWatcher.doWatch();
        logicServer.gameCenterWatcher = gameCenterWatcher;
        logicServer.startup();
        mqClient.start();
        dbServer.startup();
        assistServer.startup();
        // 注册自己
        zkClient.createOrsetData_EPHEMERAL("/logic/" + config.getId(), logicServer.loadInfo);
        logger.info(config.getTopic() + " started.");

        logicServer.join();			
        dbServer.join();
        assistServer.join();
    }

    public String getCenterTopicName(String gameName) {
        return gameCenterWatcher.getCenterTopic(gameName);
    }

    @Override
    public void process(WatchedEvent event) {

    }
}
